﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace Sync3DCartDatabase
{
    public class ServiceConfig
    {
        private int _syncInterval = 0;
        private string _folderPath = string.Empty;

        
        public string WorkingFolderPath
        {
            get
            {
                return string.Concat(_folderPath, Path.DirectorySeparatorChar, CConstants.WORKING_FOLDER, Path.DirectorySeparatorChar);
            }
            set
            {
                _folderPath = value;
                //if (!Directory.Exists(_folderPath))
                //{
                //    Directory.CreateDirectory(_folderPath);
                //    Directory.CreateDirectory(string.Concat(_folderPath, Path.DirectorySeparatorChar, CConstants.WORKING_FOLDER));
                //    Directory.CreateDirectory(string.Concat(_folderPath, Path.DirectorySeparatorChar, CConstants.ERROR_FOLDER));
                //    Directory.CreateDirectory(string.Concat(_folderPath, Path.DirectorySeparatorChar, CConstants.COMPLETED_FOLDER));
                //}
                //else
                //{
                //    Directory.CreateDirectory(string.Concat(_folderPath, Path.DirectorySeparatorChar, CConstants.WORKING_FOLDER));
                //    Directory.CreateDirectory(string.Concat(_folderPath, Path.DirectorySeparatorChar, CConstants.ERROR_FOLDER));
                //    Directory.CreateDirectory(string.Concat(_folderPath, Path.DirectorySeparatorChar, CConstants.COMPLETED_FOLDER));
                //}
            }
        }

        public string ErrorFolderPath
        {
            get
            {
                return string.Concat(_folderPath, Path.DirectorySeparatorChar, CConstants.ERROR_FOLDER, Path.DirectorySeparatorChar);
            }
        }

        public string CompletedFolderPath
        {
            get
            {
                return string.Concat(_folderPath, Path.DirectorySeparatorChar, CConstants.COMPLETED_FOLDER, Path.DirectorySeparatorChar);
            }
        }

        public int SyncInterval 
        { 
            get 
            {
                return _syncInterval;
            }
            set
            {
                _syncInterval = value * 1000;
            } 
        }

        public string SourceFolderPath { get; set; }

        public CSessionOptionCDO FTPConnectionInfo { get; set; }

        public MappedColumnsDetails MappedColumnsDetail { get; set; }

        public CartConnectionInfo cartConnectionInfo { get; set; }
    }

    [XmlRoot("CartConnectionInfo")]
    public class CartConnectionInfo
    {
        [XmlElement("StoreUrl")]
        public string StoreUrl { get; set; }

        [XmlElement("UserKey")]
        public string UserKey { get; set; }
    }

    [XmlRoot("ColumnsMappingDetails")]
    public class MappedColumnsDetails
    {
        [XmlArray("ColumnsMappingDetails")]
        [XmlArrayItem("column")]
        public MappedColumnInfo[] MappedColumns
        {
            get;
            set;
        }

        [XmlIgnore]
        public List<MappedColumnInfo> MappedColumnsList
        {
            get
            {
                return MappedColumns.ToList<MappedColumnInfo>();
            }
        }

        [XmlIgnore]
        public List<MappedColumnInfo> MappedValuesColumnsList
        {
            get
            {
                return MappedColumns.ToList<MappedColumnInfo>().FindAll(delegate(MappedColumnInfo info) { return info.IsMappedValues; });
            }
        }
    }

    public class MappedColumnInfo
    {
        [XmlAttribute("name")]
        public string ColumnName { get; set; }

        [XmlAttribute("mappedName")]
        public string MappedColumnName { get; set; }

        [XmlArray("MappedValues")]
        [XmlArrayItem("value")]
        public MappedColumnValueInfo[] MappedValues
        {
            get;
            set;
        }

        [XmlIgnore]
        public List<MappedColumnValueInfo> MappedValuesList
        {
            get
            {
                return MappedValues.ToList<MappedColumnValueInfo>();
            }
        }

        [XmlIgnore]
        public bool IsMappedValues
        {
            get
            {
                if (MappedValues == null || MappedValues.Count() == 0)
                    return false;
                else
                    return true;
            }
        }
    }
        
    public class MappedColumnValueInfo
    {
        [XmlAttribute("origValue")]
        public string OriginalValue { get; set; }

        [XmlAttribute("mappedValue")]
        public string MappedValue { get; set; }
    }
}


